package cn.aylog.springboot.controller;


import cn.aylog.common.entity.R;
import cn.aylog.springboot.entity.Subject;
import cn.aylog.springboot.entity.SubjectExcelVo;
import cn.aylog.springboot.listener.SubjectReadListener;
import cn.aylog.springboot.service.ISubjectService;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 课程科目 前端控制器
 * </p>
 *
 * @author YongTay
 * @since 2022-01-22
 */
@Api("课程管理")
@RestController
@RequestMapping("/subject")
public class SubjectController {

    @Resource
    private ISubjectService subjectService;

    @ApiOperation("课程导入")
    @PostMapping("/upload")
    public R upload(MultipartFile file) {

        try {
            EasyExcel.read(file.getInputStream(), SubjectExcelVo.class, new SubjectReadListener(subjectService)).sheet().doRead();
            return R.ok();
        } catch (IOException e) {
            e.printStackTrace();
            return R.error().message(e.getMessage());
        }
    }

    @ApiOperation("查询所有课程")
    @GetMapping("/all")
    public R listAll() {
        List<Subject> list = subjectService.list();
        return R.ok().data("data", list);
    }

    @ApiOperation("查询课程分类信息")
    @GetMapping("/queryByPid/{pid}")
    public R queryByPid(@PathVariable String pid) {
        List<Subject> list = subjectService.queryByPid(pid);
        return R.ok().data("data", list);
    }

}
